package com.example.manager.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.manager.mapper.SysRoleMapper;
import com.example.manager.mapper.SysRoleMenuMapper;
import com.example.manager.mapper.SysUserRoleMapper;
import com.example.manager.service.SysRoleService;
import com.example.model.dto.system.AssginMenuDto;
import com.example.model.dto.system.AssginRoleDto;
import com.example.model.dto.system.SysRoleDto;
import com.example.model.entity.system.SysRole;
import com.example.model.entity.system.SysRoleMenu;
import com.example.model.entity.system.SysRoleUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Page<SysRole> findByPage(SysRoleDto sysRoleDto) {

        Page<SysRole> page = new Page<>(sysRoleDto.getPageNum(), sysRoleDto.getPageSize());
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNoneEmpty(sysRoleDto.getRoleName())){
            sysRoleLambdaQueryWrapper.like(SysRole::getRoleName, sysRoleDto.getRoleName());
        }
        Page<SysRole> sysRolePage = sysRoleMapper.selectPage(page, sysRoleLambdaQueryWrapper);

        return sysRolePage;
    }

    @Override
    public void assigningRoles(AssginRoleDto assginRoleDto) {

        Long userId = assginRoleDto.getUserId();
        List<Long> roleIdList = assginRoleDto.getRoleIdList();

        LambdaQueryWrapper<SysRoleUser> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (userId != null){
            //先删除所有跟userid关联的角色
            sysUserRoleMapper.delete(sysRoleLambdaQueryWrapper.eq(SysRoleUser::getUserId, userId));
            for (Long roleId : roleIdList) {
                SysRoleUser sysRoleUser = new SysRoleUser();
                sysRoleUser.setRoleId(roleId);
                sysRoleUser.setUserId(userId);
                sysUserRoleMapper.insert(sysRoleUser);
            }
        }

    }

    @Override
    public Map<String, Object> allRoles(Long userId) {
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        List<SysRole> data = sysRoleMapper.selectList(null);
        List<Long> sysRoleUsers = new ArrayList<>();
        if (userId != null){
           sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysRoleUser>()
                    .eq(SysRoleUser::getUserId, userId)
                    .select(SysRoleUser::getRoleId)
            ).forEach(sysRoleUser -> sysRoleUsers.add(sysRoleUser.getRoleId()));
        }
        stringObjectHashMap.put("sysRoleList",data);
        stringObjectHashMap.put("sysRoleUserList",sysRoleUsers);
        return stringObjectHashMap;
    }

    @Override
    public List<Long> findMenuIdsByRoleId(Long roleId) {
        List<Long> longs = new ArrayList<>();
        sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
                .select(SysRoleMenu::getMenuId)
        ).forEach(sysRoleMenu -> longs.add(sysRoleMenu.getMenuId()));
        return longs;
    }

    @Override
    public void assigningMenus(AssginMenuDto assginMenuDto) {
        //1.先根据角色id删除所有关联的菜单
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, assginMenuDto.getRoleId())
        );

        //2.再添加新的关联关系
        for (Long menuId : assginMenuDto.getMenuIdList()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(assginMenuDto.getRoleId());
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
    }
}
